<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.5">
<TITLE>czestmyr.jjsched.PacketHandler (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">PacketHandler.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="MultiLineComment">/*  This file is a part of JJSched source code
<A NAME="2"></A>    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
<A NAME="3"></A>    Copyright (C) 2008 Cestmir "CZestmyr" Houska
<A NAME="4"></A>
<A NAME="5"></A>    This program is free software: you can redistribute it and/or modify
<A NAME="6"></A>    it under the terms of the GNU General Public License as published by
<A NAME="7"></A>    the Free Software Foundation, either version 3 of the License, or
<A NAME="8"></A>    (at your option) any later version.
<A NAME="9"></A>
<A NAME="10"></A>    This program is distributed in the hope that it will be useful,
<A NAME="11"></A>    but WITHOUT ANY WARRANTY; without even the implied warranty of
<A NAME="12"></A>    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
<A NAME="13"></A>    GNU General Public License for more details.
<A NAME="14"></A>
<A NAME="15"></A>    You should have received a copy of the GNU General Public License
<A NAME="16"></A>    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt; */</FONT>
<A NAME="17"></A>
<A NAME="18"></A><FONT ID="Package">package</FONT> czestmyr.jjsched;
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="Import">import</FONT> java.util.*;
<A NAME="21"></A><FONT ID="Import">import</FONT> java.lang.*;
<A NAME="22"></A>
<A NAME="23"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.*;
<A NAME="24"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.packet.*;
<A NAME="25"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.util.*;
<A NAME="26"></A>
<A NAME="27"></A><FONT ID="FormalComment">/**
<A NAME="28"></A> * Handles the incoming message packets.
<A NAME="29"></A> */</FONT>
<A NAME="30"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> PacketHandler <FONT ID="Implements">implements</FONT> PacketListener{
<A NAME="31"></A>    <FONT ID="FormalComment">/** Constructor */</FONT>
<A NAME="32"></A>    PacketHandler(<A HREF="../../czestmyr/jjsched/Connector.java.html">Connector</A> c, <A HREF="../../czestmyr/jjsched/Config.java.html">Config</A> cfg, <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A> dbc, <A HREF="../../czestmyr/jjsched/TimeHandler.java.html">TimeHandler</A> th, <A HREF="../../czestmyr/jjsched/Macros.java.html">Macros</A> ms, <A HREF="../../czestmyr/jjsched/CommandParser.java.html">CommandParser</A> cp, <A HREF="../../czestmyr/jjsched/ActionExecutor.java.html">ActionExecutor</A> ce){
<A NAME="33"></A>        theConnector = c;
<A NAME="34"></A>        theConfig = cfg;
<A NAME="35"></A>        theDBConnector = dbc;
<A NAME="36"></A>        theTime = th;
<A NAME="37"></A>        theMacros = ms;
<A NAME="38"></A>        theParser = cp;
<A NAME="39"></A>        theExecutor = ce;
<A NAME="40"></A>    }
<A NAME="41"></A>
<A NAME="42"></A>    <FONT ID="FormalComment">/**
<A NAME="43"></A>     * Processes the incoming packet.
<A NAME="44"></A>     * The message from the packet is translated from the user encoding, macros are substituted
<A NAME="45"></A>     * and the packet is then parsed and the corresponding action executed.
<A NAME="46"></A>     *
<A NAME="47"></A>     * @param   packet  The incoming packet that should be processed
<A NAME="48"></A>     */</FONT>
<A NAME="49"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> processPacket(Packet packet){
<A NAME="50"></A>        <FONT ID="SingleLineComment">//Debug
<A NAME="51"></A></FONT>        System.out.println(<FONT ID="StringLiteral">"packet received"</FONT>);
<A NAME="52"></A>
<A NAME="53"></A>        String message = ((Message)packet).getBody();
<A NAME="54"></A>
<A NAME="55"></A>        <FONT ID="SingleLineComment">//Debug
<A NAME="56"></A></FONT>        System.out.println(message);
<A NAME="57"></A>
<A NAME="58"></A>        String unescapedUser = StringUtils.parseBareAddress(packet.getFrom());
<A NAME="59"></A>
<A NAME="60"></A>        <FONT ID="Try">try</FONT> {
<A NAME="61"></A>            <FONT ID="SingleLineComment">//String encoding = theDBConnector.getUserCharset(unescapedUser);
<A NAME="62"></A></FONT>            <FONT ID="SingleLineComment">//message = new String(message.getBytes(), encoding);
<A NAME="63"></A></FONT>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="64"></A>            <FONT ID="SingleLineComment">//Do nothing - i.e. leave the characters in their original encoding
<A NAME="65"></A></FONT>            <FONT ID="SingleLineComment">//Debug
<A NAME="66"></A></FONT>            System.out.println(e.getMessage());
<A NAME="67"></A>        }
<A NAME="68"></A>
<A NAME="69"></A>        <FONT ID="SingleLineComment">//Debug
<A NAME="70"></A></FONT>        System.out.println(<FONT ID="StringLiteral">"parsing packet"</FONT>);
<A NAME="71"></A>        System.out.println(message);
<A NAME="72"></A>
<A NAME="73"></A>        LinkedList&lt;String&gt; tokens = StringParser.parseLine(message);
<A NAME="74"></A>
<A NAME="75"></A>        <FONT ID="SingleLineComment">//Macro expansion
<A NAME="76"></A></FONT>        ListIterator it = tokens.listIterator(<FONT ID="IntegerLiteral">0</FONT>);
<A NAME="77"></A>        <FONT ID="While">while</FONT> (it.hasNext()){
<A NAME="78"></A>            String token = (String)it.next();
<A NAME="79"></A>            <FONT ID="If">if</FONT> (theMacros.isMacro(token)){
<A NAME="80"></A>                it.remove();
<A NAME="81"></A>                Iterator macroit = theMacros.getMacroBody(token).iterator();
<A NAME="82"></A>                <FONT ID="While">while</FONT> (macroit.hasNext()){
<A NAME="83"></A>                    it.add((String)macroit.next());
<A NAME="84"></A>                }
<A NAME="85"></A>            }
<A NAME="86"></A>        }
<A NAME="87"></A>
<A NAME="88"></A>        String user = StringParser.escapeJID(unescapedUser);
<A NAME="89"></A>
<A NAME="90"></A>        <A HREF="../../czestmyr/jjsched/Action.java.html">Action</A> action = theParser.parse(tokens, unescapedUser, user);
<A NAME="91"></A>        theExecutor.execute(action);
<A NAME="92"></A>    }
<A NAME="93"></A>
<A NAME="94"></A>    <FONT ID="SingleLineComment">//References to several classes that this class needs for processing the incoming packets
<A NAME="95"></A></FONT>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Connector.java.html">Connector</A> theConnector;
<A NAME="96"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Config.java.html">Config</A> theConfig;
<A NAME="97"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A> theDBConnector;
<A NAME="98"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/TimeHandler.java.html">TimeHandler</A> theTime;
<A NAME="99"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Macros.java.html">Macros</A> theMacros;
<A NAME="100"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/CommandParser.java.html">CommandParser</A> theParser;
<A NAME="101"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/ActionExecutor.java.html">ActionExecutor</A> theExecutor;
<A NAME="102"></A>}
<A NAME="103"></A>
<A NAME="104"></A></pre>
</BODY>
</HTML>